<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>iterator</title>
</head>
<body>
    

    <script>
    
        /* {
            let ary = ['hello','world'];

            let map = ary[Symbol.iterator]();

            console.log(map.next());
            console.log(map.next());
            console.log(map.next());
        } */


        {
            // 自定义 使用iterator接口

            let obj = {
                s: [1,2,3],
                e: [4,5,6],
                [Symbol.iterator] () {
                    let _this = this;
                    let index = 0;
                    let ary = _this.s.concat(_this.e);
                    let len = ary.length;
                    return {
                        next () {
                            if (index<len) {
                                return {
                                    value: ary[index++],
                                    done: false
                                }
                            } else {
                                return {
                                    value: ary[index++],
                                    done: true
                                }
                            }
                        }
                    } 
                }
            };

            for (const key of obj) {
                console.log(key); // 1 2 3 4 5 6
            }

        }
    
    </script>
</body>
</html>